package com.shishuo.cms.filter;

import java.util.Locale;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.MDC;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.i18n.LocaleContextHolder;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.i18n.SessionLocaleResolver;

import com.shishuo.cms.service.ConfigService;
import com.shishuo.cms.util.HttpUtils;

@Component
public class GlobalInterceptor implements HandlerInterceptor {
	@Autowired
	private ConfigService configService;

	@Override
	public boolean preHandle(HttpServletRequest request,
			HttpServletResponse response, Object handler) throws Exception {
		// 根据请求配置国际化类型
		String langType = request.getParameter("langType");
		if (null != langType) {
			if (langType.equals("zh")) {
				Locale locale = new Locale("zh", "CN");
				request.getSession().setAttribute(
						SessionLocaleResolver.LOCALE_SESSION_ATTRIBUTE_NAME,
						locale);
			} else if (langType.equals("en")) {
				Locale locale = new Locale("en", "US");
				request.getSession().setAttribute(
						SessionLocaleResolver.LOCALE_SESSION_ATTRIBUTE_NAME,
						locale);
			} else
				request.getSession().setAttribute(
						SessionLocaleResolver.LOCALE_SESSION_ATTRIBUTE_NAME,
						LocaleContextHolder.getLocale());
		}
		return true;
	}

	@Override
	public void postHandle(HttpServletRequest request,
			HttpServletResponse response, Object handler,
			ModelAndView modelAndView) throws Exception {
		if (null == modelAndView) {
			return;
		}
		// 系统配置参数
		String basePath = HttpUtils.getBasePath(request);
		String contextPath = HttpUtils.getContextPath(request);
		modelAndView.addObject("basePath", basePath);
		modelAndView.addObject("contextPath", contextPath);
		modelAndView.addObject("SYS_SITEDESC",
				configService.getConfigByKey("SYS_SITEDESC"));
		modelAndView.addObject("SYS_SITENAME",
				configService.getConfigByKey("SYS_SITENAME"));
		modelAndView.addObject("SYS_TEMPLATE",
				configService.getConfigByKey("SYS_THEME"));
		modelAndView.addObject("TEMPLATE_PATH", basePath + "/template/"
				+ configService.getConfigByKey("SYS_THEME"));
		modelAndView.addObject("IS_CACHE_HTML",
				configService.getConfigByKey("IS_CACHE_HTML"));
		modelAndView.addObject("SYS_KEYWORDS",
				configService.getConfigByKey("SYS_KEYWORDS"));
		double callbackPercent=Double.parseDouble(("".equals(configService.getConfigByKey("SYS_CALLBACK_PERCENT"))?"100":configService.getConfigByKey("SYS_CALLBACK_PERCENT")));
		modelAndView.addObject("SYS_CALLBACK_PERCENT",callbackPercent);
		MDC.put("ip", HttpUtils.getIp(request));

	}

	@Override
	public void afterCompletion(HttpServletRequest request,
			HttpServletResponse response, Object handler, Exception ex)
			throws Exception {
	}
}
